package mail.gui;

import crypt.KeyHandler;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import mail.util.Mail;

/**
 * The main frame of the GUI. Designed as a singleton.
 *
 * @author Jonas.Lindmark
 */
public class MainFrame extends JFrame implements ActionListener{
    private JDesktopPane desktop;
    private static MainFrame instance = null;
    public static MainFrame getInstance()
    {
        return instance;
    }
    /**
     * Create a new default MainFrame
     */
    private MainFrame()
    {
        super("Main Frame");
        int inset = 100;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(inset, inset,Math.min(screenSize.width-inset*2,800),Math.min(screenSize.height-inset*2,600));
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        desktop = new JDesktopPane();
        setContentPane(desktop);
        setJMenuBar(getTheMenu());
    }
    /**
     * Adds a given component to the desktop
     * 
     * @param c The component
     */
    public void addToDesktop(Component c)
    {
        desktop.add(c);
    }
    /**
     * Handles events
     * 
     * @param e 
     */
    public void actionPerformed(ActionEvent e)
    {
        final String action = e.getActionCommand();
        if (action.equals("send"))
        {
            openSend();
        } else if (action.equals("rec"))
        {
            openRecieve();
        } else if (action.equals("newKeys"))
        {
            genKeys();
        } else {
            System.exit(0);
        }
    }
    /**
     * Warning! Does not handle files already existing, will overwrite.
     */
    private void genKeys()
    {
        String pubFile = null;
        while (pubFile == null)
            pubFile = JOptionPane.showInputDialog("Please enter name of public file");
        String privFile = null;
        while(privFile == null)
            privFile = JOptionPane.showInputDialog("Please enter name of private file");
        new KeyHandler().genKeys(privFile, pubFile);
    }
    /**
     * Create a new menu and returns it
     * 
     * @return the menu
     */
    private JMenuBar getTheMenu()
    {
        JMenuBar bar = new JMenuBar();
        JMenu menu = new JMenu("File");
        bar.add(menu);

        JMenuItem send = new JMenuItem("send");
        send.addActionListener(this);
        send.setActionCommand("send");
        JMenuItem rec  = new JMenuItem("recieve");
        rec.addActionListener(this);
        rec.setActionCommand("rec");
        JMenuItem quit = new JMenuItem("quit");
        quit.addActionListener(this);
        quit.setActionCommand("quit");

        JMenu crypt = new JMenu("Security");
        bar.add(crypt);
        JMenuItem keys = new JMenuItem("Create new key-pair");
        keys.addActionListener(this);
        keys.setActionCommand("newKeys");
        crypt.add(keys);        

        menu.add(send);
        menu.add(rec);
        menu.add(quit);
        
        return bar;
    }
    /**
     * Opens the send window
     */
    private void openSend()
    {
        JInternalFrame main = new MailSendFrame();
        main.setVisible(true);
        desktop.add(main);
        try{
            main.setSelected(true);
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    /**
     * Opens recieve window
     */
    private void openRecieve()
    {
        JInternalFrame main = new MailReadFrame();
        main.setVisible(true);
        desktop.add(main);
        try{
            main.setSelected(true);
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * Initiates the singleton.
     */
    public static void createAndShowGUI()
    {
        if (instance == null)
            instance = new MainFrame();
    }
}
